<?php
/**
 * Controller manage email
 *
 * @author Pavel Savchuk
 */
class Backend_EmailController extends RCMS_Core_BaseController {

    protected $_websiteUrl;

    protected $_smtpHost;

    protected $_smtpLogin;

    protected $_smtpPassword;

    protected $_adminEmail;

    protected $_smtpConfigArray;
    
    protected $_configTableArray;




    public function init() {
        $this->_model = new Default_Model_EmailModel();
        $this->_websiteUrl = unserialize(Zend_Registry::get('config'))->website->website->url;
        parent::init();
        $configArray = $this->_model->selectAllConfigSettings();
        if($configArray) {
            if(isset($configArray['smtp_host']) && $configArray['smtp_host']!="" ) {
                $this->_smtpHost = $configArray['smtp_host'];
            }

            if(isset($configArray['smtp_login']) && $configArray['smtp_login']!="" ) {
                $this->_smtpLogin = $configArray['smtp_login'];
            }

            if(isset($configArray['smtp_password']) && $configArray['smtp_password']!="" ) {
                $this->_smtpPassword = $configArray['smtp_password'];
            }

            if(isset($configArray['admin_email']) && $configArray['admin_email']!="" ) {
               // $this->_adminEmail = $configArray['admin_email'];
               $this->_adminEmail = "david@vidax.com";
            }

            $this->_smtpConfigArray = array(
                'auth' => 'login',
                'username' => $this->_smtpLogin,
                'password' => $this->_smtpPassword,
                'host' => $this->_smtpHost
            );

        }
    }

    public function sendAction() {
        $this->_helper->getHelper('layout')->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        var_dump($this->_smtpHost);
        var_dump($this->_smtpConfigArray);
        var_dump($this->_adminEmail);

        $tr = new Zend_Mail_Transport_Smtp($this->_smtpHost, $this->_smtpConfigArray);
        Zend_Mail::setDefaultTransport($tr);
        $mailer = new Zend_Mail( 'utf-8' );
        $mailer->setBodyText("Body TEXT");
        $mailer->setFrom('photoadmin@phototoaster.com');
        $mailer->addTo("pavel@seosamba.com");
        $mailer->setSubject('TEST SMTP');
        $mailer->send();
        
        /*try {
            if (!$mailer->send()) {
                throw new Exception("Error occurred sending message");
            }else {
                return "send";
            }
        }
        catch (Exception $e) {
            return "failed";
        }*/
    }

    }